
import {Link} from 'react-router-dom';
import {Form, Input, Icon, Breadcrumb, Button, Alert, Select } from 'antd';
const Option = Select.Option;
const FormItem = Form.Item;

import * as Actions from './action'
import Utils from '../../common/utils/utils'
//import {USER_STATUS} from '../../common/utils/constants'
import * as CONSTANTS from '../../common/utils/constants'

class Detail extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      is_not_add: false, // 添加模式、编辑模式的判断
      detail: {},
      confirmDirty: false,
      buttonLoading: false,
      roleCode: [],
      roleList: [],
        identi:[]
    }
  }
    componentWillMount(){
        let _this = this;
        Actions.getChannel().then(res => {
            // console.log(res);
            if(res.errorCode === 0){
                _this.setState({
                    roleList:res.body
                })
            }
        });
        let params = {limit: 10, page: 0};
        Actions.getRoleList(params).then((result) => {
            if(result.errorCode === 0) {
                // console.warn(result.body);
                this.setState({
                    identi: result.body.rows
                });
            }else {
                Utils.dialog.error(result.msg)
            }
        });
    }
  componentDidMount() {
    let _this = this, {dispatch, match} = this.props, id = match.params.id;
    // if (id !== undefined) {
    //   this.setState({
    //     is_not_add: true
    //   })

      match.url.indexOf('detail') > 0 && this.getDetails(id);
      //match.url.indexOf('edit') > 0 && this.getDetails(id);
    // }
  }
  getDetails = (id) => {
      let page = this;
      Actions.getUserDetail(id).then(response => {
          if (response.errorCode === 0) {
              console.log('1',response);
            page.setState({
                detail:response.body
            })
          } else {
              Utils.dialog.error(response.msg)
          }
      })
  }

  handleSubmit = (e) => {
    e.preventDefault()
    let {match, history} = this.props, _this = this
    this.props.form.validateFieldsAndScroll((err, values) => {
      if(!err) {
        _this.setState({
          buttonLoading: true
        })
          console.log(values);
        if(values.permission) {
          values.permission = values.permission.join(',')
        }
        if(values.password) {
          values.password = Utils.sha256_digest(values.password);
        }
        delete values.confirm

        if (!match.params.id) {
          Actions.addAdmin(values).then((response) => {

            _this.setState({
              buttonLoading: false
            })
            if (response.errorCode == 0) {
              Utils.dialog.success('添加成功', () => {
                history.push('/admin/user/index')
              })
            } else {
              Utils.dialog.error(response.msg);
            }
          },(response) => {

              _this.setState({
                  buttonLoading: false
              })
          })
        } else{
          values.id = match.params.id
          Actions.editAdmin(values).then(response => {
            _this.setState({
              buttonLoading: false
            })
            if (response.errorCode == 0) {
              Utils.dialog.success('修改成功', () => {
                  if (match.params.type == 'password') {
                    history.push('/admin/user/index')
                  }
                }
              )

            } else {
              Utils.dialog.error(response.msg);
                _this.setState({
                    buttonLoading: false
                })
            }
          },(response) => {

              _this.setState({
                  buttonLoading: false
              })
          })
        }
      }
    });
  }

  handleConfirmBlur = (e) => {
    const value = e.target.value;
    this.setState({confirmDirty: this.state.confirmDirty || !!value});
  }

  checkPassword = (rule, value, callback) => {
    const form = this.props.form;
    if (value && value !== form.getFieldValue('password')) {
      callback('两次密码输入不一致!');
    } else {
      callback();
    }
  }

  checkConfirm = (rule, value, callback) => {
    const form = this.props.form;
    if (value && this.state.confirmDirty) {
      form.validateFields(['confirm'], {force: true});
    }
    callback();
  }

  render() {
    const {getFieldDecorator} = this.props.form;
    const {match} = this.props;
    const {detail, is_not_add, roleCode, roleList,identi} = this.state
    const formItemLayout = {
      labelCol: {
        xs: {span: 24},
        sm: {span: 6},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 14},
      },
    };
    const tailFormItemLayout = {
      wrapperCol: {
        xs: {
          span: 24,
          offset: 0,
        },
        sm: {
          span: 14,
          offset: 6,
        },
      },
    };
      return (
        <div id="wrap">
          <Breadcrumb className="breadcrumb">
            <Breadcrumb.Item>
              <Link to='/admin/userlist/index'>
                <Icon type="user"/>
                <span>用户管理列表</span>
              </Link>
            </Breadcrumb.Item>
            <Breadcrumb.Item>查看详情</Breadcrumb.Item>
          </Breadcrumb>
          <div className="content">
            <Form onSubmit={this.handleSubmit.bind(this)}>
              <div>
                <FormItem
                  {...formItemLayout}
                  label="用户名称"
                >
                 <div>{detail.userName}</div>
                </FormItem>
                <FormItem
                  {...formItemLayout}
                  label="创建时间"
                >
                  <div>{Utils.formatDate(detail.createdTime, 'YYYY-MM-DD hh:ii')}</div>
                </FormItem>
                  <FormItem
                      {...formItemLayout}
                      label="状态"
                  >
                      <div>{CONSTANTS.USER_STATUS[detail.status]}</div>
                  </FormItem>
                  <FormItem
                      {...formItemLayout}
                      label="性别"
                  >
                      <div>{CONSTANTS.SEX[detail.gender]}</div>
                  </FormItem>
                  {detail.apply!= null && <div>
                  <FormItem
                      {...formItemLayout}
                      label="申请名字"
                  >
                      <div>{detail.apply.name}</div>
                  </FormItem>
                  <FormItem {...formItemLayout} label="申请电话">
                  <div>{detail.apply.phone}</div>
                  </FormItem>
                  <FormItem {...formItemLayout} label="审核状态">
                  <div>{detail.apply.auditStatus=='REFUSE'?'申请不通过':detail.apply.auditStatus=='AUDITED'?'审核通过':'待审核'}</div>
                  </FormItem>
                  <FormItem {...formItemLayout} label="支付状态">
                  <div>{detail.apply.payStatus==0?'未支付':detail.apply.payStatus==1?'全款支付':detail.apply.payStatus==2?'支付第一笔':detail.apply.payStatus==3?'支付第二笔':detail.apply.payStatus==4?'免支付':'线下支付'}</div>
                  </FormItem>
                      </div>
                  }




              </div>
            </Form>
          </div>
        </div>
      )

  }
}

const DetailForm = Form.create()(Detail)
export default DetailForm